/**
 * 提料申请
 * Created by douxc on 2015/4/22.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('FeedApplyCtrl', ['$scope', '$routeParams', 'MHService', 'Upload',
      function ($scope, $routeParams, MHService, Upload) {
        if (MHService.cacheData(false, {key: 'map'})) {
          location.reload();
        }
        var imgUrl = [];//图片路径
        //获取项目
        MHService.getReq({url: MHService.URL.feed_apply}, function (data) {
          $scope.projects = data['datas'];
          $scope.selected = $scope.projects[0];
        });
        /**
         * 提交料单
         */
        $scope.submit = function () {
          /*表单验证*/
          if (!$scope.title) {
            MHService.Toast('请填写料单标题');
            return;
          }
          if (!$scope.codenum) {
            MHService.Toast('请填写料单编号');
            return;
          }
          if (!$scope.description) {
            MHService.Toast('请填写料单说明');
            return;
          }
          //if (imgUrl.length <= 0) {
          //  MHService.Toast('请上传图片附件');
          //  return;
          //}
          var feedList = MHService.globalData(false, {key: 'feedList'});
          var feeds = [];
          for (var index in feedList) {
            feeds.push(feedList[index].submit);
          }
          //需要提交的数据
          var _req = new FormData();
          _req.append('title', $scope.title);
          _req.append('code', $scope.codenum);
          _req.append('gardenid', $scope.selected.id);
          _req.append('description', $scope.description);
          _req.append('url', imgUrl);
          _req.append('datas', feeds.join(','));
          //MHService.postReq({url: MHService.URL.feed_submit, _req: req}, function (data) {
          //  MHService.Toast('提交成功');
          //});
          $.ajax({
            type: 'POST',
            url: MHService.URL.feed_submit,
            data: _req,
            processData: false,
            contentType: false,
            complete: function (data) {
              console.log(data.responseText);
              if (data.responseText.match('"success":true')) {
                MHService.Toast('提交成功');
                //清空材料列表
                MHService.removeStorage('feedList').then(function () {
                    history.go(-1);
                  }, function () {
                    MHService.Toast('材料清空失败');
                  }
                );
              } else {
                MHService.Toast('提交失败，请刷新重试');
              }
            }
          });
        };
        /**
         * 选中了文件
         * @param $files
         */
        $scope.onFileSelect = function ($files) {
          console.log($files);
          if ($files.length > 0) {
            Upload.upload({url: MHService.URL.img_upload, method: 'POST', file: $files[0]}).success(function (res) {
              //文件上传完成
              console.log(res);
              alert(JSON.stringify(res.data));
              imgUrl.push(res.data);
              //$scope.images.push('../' + res.data.path);
              //imgUrl  用于存储图片路径的变量，这里要根据后台需要的参数格式格式化后赋值给imgUrl
            });
          }
        };
      }
    ])
    ;
  };
})
;
